/*
  multer 配置项 
*/

const multer = require("multer");
const mkdirp = require("mkdirp");
const path = require("path");

const storage = multer.diskStorage({
  // 配置上传的目录
  destination: async (req, file, cb) => {
    console.log("multer.diskStorage--------------",req,'--------------file:',file,'--------------------cb',cb)
    // 生成格式化日期
    let date = String(new Date().getTime());
    // 获取目录路径
    let dir = path.join("static/uploads", date);
    // 生成目录，异步改同步
    await mkdirp(dir);
    cb(null, dir);
  },
  filename: function (res, file, cb) {
    // 获取后缀名
    let extname = path.extname(file.originalname);
    // 根据时间戳生成文件名
    cb(null, Date.now() + extname);
  },
});

// 为 multer 添加配置
const multerConfig = multer({
  storage,
  limits: { fileSize: 2097152 }, //文件大小限制2m
});

module.exports = multerConfig;
